package com.stox.controller;

import java.util.List;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.stox.dto.web.Response;
import com.stox.manager.CountryManager;
import com.stox.model.Country;

@RestController
@RequestMapping(value="/countries")
public class CountryController implements InitializingBean{

	@Autowired
	private CountryManager countryManager;
	
	@Override
	public void afterPropertiesSet() throws Exception {
		if(0 == countryManager.count()){
			Country country = new Country();
			country.setId(1l);
			country.setName("India");
			country.setFlageImagePath("Flag_of_India.png");
			countryManager.save(country);
		}
	}
	
	@ResponseBody
	@Cacheable(value=CountryManager.cacheName, key="'CountryController-findAll'")
	@RequestMapping(method=RequestMethod.GET, produces="application/json")
	public Response<List<Country>> findAll(){
		return new Response<>(countryManager.findAll()); 
	}
	
}
